package com.yanggu.mapreduce.elk;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/8 14:30
 * @description:
 */
public class ELKMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    
    private Text k = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1. 获取一行
        String line = value.toString();

        //2. 判断是否合法
        boolean result = parseLog(line, context);

        if (!result) {
            return;
        }

        //3. 进行写出
        k.set(line);
        context.write(k, NullWritable.get());
    }

    private boolean parseLog(String line, Context context) {
        //1. 按照\t进行截取
        String[] fields = line.split(" ");

        //2. 进行判断
        if (fields.length < 11) {
            context.getCounter("map", "false").increment(1);
            return false;
        } else {
            context.getCounter("map", "true").increment(1);
            return true;
        }
    }
}
